package eHealth.android.client;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import eHealth.android.Helpers.MedicalDataItem;
import eHealth.android.Helpers.StatisticsHelper;

/**
 * 
 * @author Thomas Rouvinez
 * Date 08.03.2012
 *
 * Goal : Display the previously baked statistics.
 *
 */
public class Statistics extends Activity implements OnClickListener, OnItemSelectedListener{
	
	// ----------------------------------------------------------------------------------------------
	// Variables.
	// ----------------------------------------------------------------------------------------------
	
	/**
	 * Storage unit for retrieved medical data during the loading process.
	 */
	public static MedicalDataItem[] data;
	
	/**
	 * Storage unit for statistics.
	 */
	public static StatisticsHelper[] statistics;
	
	/**
	 * The kind of time range the user has selected (default = 0).
	 */
	private int selectedTimeLaps = 0;
	
	/**
	 * The kind of category the user has selected (default = 1).
	 */
	private int categorySelected = 1;
	
	/**
	 * Boolean value to know what kind of statistics we have to display (global vs. categorical).
	 */
	private boolean globalMean = true;

	/**
	 * RadioButton to choose overall statistics.
	 */
	private RadioButton globalRadioButton;
	
	/**
	 * RadioButton to choose category statistics.
	 */
	private RadioButton categoryRadioButton;

	/**
	 * Spinner to display the choice of time ranges.
	 */
	private Spinner timeLaps;
	
	/**
	 * Spinner to display the choice of categories.
	 */
	private Spinner category;

	/**
	 * TextView to display the statistics results for the kind of statistics currently displayed.
	 */
	private TextView statisticsTitle;
	
	/**
	 * TextView to display the statistics results for the diastolic pressure.
	 */
	private TextView pressureDia;
	
	/**
	 * TextView to display the statistics results for the systolic pressure.
	 */
	private TextView pressureSys;
	
	/**
	 * TextView to display the statistics results for the blood sugar.
	 */
	private TextView bloodSugar;
	
	/**
	 * TextView to display the statistics results for the humalog insulin quantity.
	 */
	private TextView humalog;
	
	/**
	 * TextView to display the statistics results for the novorapid insulin quantity.
	 */
	private TextView novorapid;
	
	/**
	 * TextView to display the statistics results for the weight.
	 */
	private TextView weight;
	
	/**
	 * TextView to display the statistics results for the pulse rate.
	 */
	private TextView pulseRate;

	// ----------------------------------------------------------------------------------------------
	// Android methods.
	// ----------------------------------------------------------------------------------------------

	/** Called when the activity is first created.<br>
	 * Set the window size to fullscreen and assign the layout - GUI  */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set full screen application.
		requestWindowFeature(Window.FEATURE_NO_TITLE);   
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		// Set content view.
		setContentView(R.layout.statistics);

		// Assign the buttons.
		assignButtons();
	}

	/** Called when the activity starts once it is created. <br>
	 * Displays statistics for the first time with default presets*/
	@Override
	protected void onStart(){
		super.onStart();

		// Add listeners to the buttons.
		setListeners();

		displayGlobalStatistics(selectedTimeLaps);
	}

	// ----------------------------------------------------------------------------------------------
	// Methods.
	// ---------------------------------------------------------------------------------------------- 

	/**
	 * Link local graphical objects to GUI objects.
	 */
	private void assignButtons(){
		globalRadioButton = (RadioButton)findViewById(R.id.radioButton_Global);
		categoryRadioButton = (RadioButton)findViewById(R.id.radioButton_Category);

		timeLaps = (Spinner)findViewById(R.id.spinner_StatisticTimeLaps);
		category = (Spinner)findViewById(R.id.spinner_StatisticCategory);
		category.setSelection(categorySelected);

		statisticsTitle = (TextView)findViewById(R.id.textView_StatisticsTitle);
		pressureDia = (TextView)findViewById(R.id.textView_StatisticsBPressureD);
		pressureSys = (TextView)findViewById(R.id.textView_StatisticsBPressureS);
		bloodSugar = (TextView)findViewById(R.id.textView_StatisticsBloodSugar);
		humalog = (TextView)findViewById(R.id.textView_StatisticsHumalog);
		novorapid = (TextView)findViewById(R.id.textView_StatisticsNovorapid);
		weight = (TextView)findViewById(R.id.textView_StatisticsWeight);
		pulseRate = (TextView)findViewById(R.id.textView_StatisticsPulseRate);
	}

	/**
	 * Display global statistics in the GUI.
	 * @param timeLaps
	 */
	private void displayGlobalStatistics(int timeLaps){
		statisticsTitle.setText(getString(R.string.RadioButtonGlobally) + " " + getString(R.string.TextViewStatisticTitle));

		pressureDia.setText(getString(R.string.TextViewStatisticInfoPressureD) + " " +  formatMin(statistics[timeLaps].getMin()[1]) + " / " 
				+ formatMean(statistics[timeLaps].getMean()[1]) + " / " + formatMax(statistics[timeLaps].getMax()[1]));

		pressureSys.setText(getString(R.string.TextViewStatisticInfoPressureS) + " " + formatMin(statistics[timeLaps].getMin()[0]) + " / "
				+ formatMean(statistics[timeLaps].getMean()[0]) + " / " + formatMax(statistics[timeLaps].getMax()[0]));

		bloodSugar.setText(getString(R.string.TextViewStatisticInfoBloodSugar) + " " + formatMin(statistics[timeLaps].getMin()[2]) + " / "
				+ formatMean(statistics[timeLaps].getMean()[2]) + " / " + formatMax(statistics[timeLaps].getMax()[2]));

		humalog.setText(getString(R.string.TextViewStatisticInfoHumalog) + " " + formatMin(statistics[timeLaps].getMin()[3]) + " / "
				+ formatMean(statistics[timeLaps].getMean()[3]) + " / " + formatMax(statistics[timeLaps].getMax()[3]));

		novorapid.setText(getString(R.string.TextViewStatisticInfoNovorapid) + " " + formatMin(statistics[timeLaps].getMin()[4]) + " / "
				+ formatMean(statistics[timeLaps].getMean()[4]) + " / " + formatMax(statistics[timeLaps].getMax()[4]));	

		weight.setText(getString(R.string.TextViewStatisticInfoWeight) + " " + formatMin(statistics[timeLaps].getMin()[5]) + " / "
				+ formatMean(statistics[timeLaps].getMean()[5]) + " / " + formatMax(statistics[timeLaps].getMax()[5]));

		pulseRate.setText(getString(R.string.TextViewStatisticInfoPulseRate) + " " + formatMin(statistics[timeLaps].getMin()[6]) + " / "
				+ formatMean(statistics[timeLaps].getMean()[6]) + " / " + formatMax(statistics[timeLaps].getMax()[6]));
	}

	/**
	 * Display category statistics.
	 * @param timeLaps
	 * @param selectedCategory
	 */
	private void displayCategoryStatistics(int timeLaps, int selectedCategory){
		statisticsTitle.setText(getString(R.string.RadioButtonCategory) + " " + getString(R.string.TextViewStatisticTitle));

		bloodSugar.setText(getString(R.string.TextViewStatisticInfoBloodSugar) + " "
				+ formatMin(statistics[timeLaps].getCategoryStatistics()[0][categorySelected].getMinCategory()) + " / "
				+ formatMean(statistics[timeLaps].getCategoryStatistics()[0][categorySelected].getMeanCategory()) + " / "
				+ formatMax(statistics[timeLaps].getCategoryStatistics()[0][categorySelected].getMaxCategory()) + " / ");
		
		humalog.setText(getString(R.string.TextViewStatisticInfoHumalog) + " "
				+ formatMin(statistics[timeLaps].getCategoryStatistics()[1][categorySelected].getMinCategory()) + " / "
				+ formatMean(statistics[timeLaps].getCategoryStatistics()[1][categorySelected].getMeanCategory()) + " / "
				+ formatMax(statistics[timeLaps].getCategoryStatistics()[1][categorySelected].getMaxCategory()) + " / ");

		novorapid.setText(getString(R.string.TextViewStatisticInfoNovorapid) + " "
				+ formatMin(statistics[timeLaps].getCategoryStatistics()[2][categorySelected].getMinCategory()) + " / "
				+ formatMean(statistics[timeLaps].getCategoryStatistics()[2][categorySelected].getMeanCategory()) + " / "
				+ formatMax(statistics[timeLaps].getCategoryStatistics()[2][categorySelected].getMaxCategory()) + " / ");
		
		
		pressureSys.setText("");
		pressureDia.setText("");
		weight.setText("");
		pulseRate.setText("");
	}

	/**
	 * Method to display right statistics in the GUI.
	 */
	private void displayStatistics() {
		if(globalMean == true){
			displayGlobalStatistics(selectedTimeLaps);
		}
		else{
			displayCategoryStatistics(selectedTimeLaps, categorySelected);
		}
	}

	/**
	 * Methods to format output of statistics minimum values.
	 * @param min
	 * @return String with correct value.
	 */
	private String formatMin(double min){
		if(Double.isNaN(min) || min == 999.0){
			return " - ";
		}

		return "" + min;
	}

	/**
	 * Methods to format output of statistics mean values.
	 * @param mean
	 * @return String with correct value.
	 */
	private String formatMean(double mean){
		if(Double.isNaN(mean)){
			return " - ";
		}

		return "" + mean;
	}

	/**
	 * Methods to format output of statistics maximum values.
	 * @param max
	 * @return String with correct value.
	 */
	private String formatMax(double max){
		if(Double.isNaN(max) || max == -999.0){
			return " - ";
		}

		return "" + max;
	}

	/**
	 * Set local listener for buttons.
	 */
	private void setListeners(){
		globalRadioButton.setOnClickListener(this);
		categoryRadioButton.setOnClickListener(this);
		timeLaps.setOnItemSelectedListener(this);
		category.setOnItemSelectedListener(this);
	}

	/**
	 * Buttons listeners.
	 */
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.radioButton_Global:
			globalMean = true;

			displayStatistics();
			category.setVisibility(View.GONE);
			break;

		case R.id.radioButton_Category:
			globalMean = false;

			displayStatistics();
			category.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}	
	}

	/**
	 * Spinner listeners.
	 */
	public void onItemSelected(AdapterView<?> spinnerId, View view, int position, long id) {
		switch (spinnerId.getId()) {
		case R.id.spinner_StatisticTimeLaps:
			this.selectedTimeLaps = position;

			displayStatistics();
			break;

		case R.id.spinner_StatisticCategory:
			this.categorySelected = position;

			displayStatistics();
			break;

		default:
			break;
		}
	}

	/**
	 * Spinner listener if nothing is selected (not used in the current context but mandatory).
	 */
	public void onNothingSelected(AdapterView<?> arg0) {}
}